package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.note.NoteDao;
import com.his.struts.dao.note.NoteImp;
import com.his.struts.form.NoteForm;
import com.his.struts.form.RyForm;
import com.his.struts.util.PageUtil;

/** 
 * 管理员查看日记
 * Creation date: 05-28-2011
 * 
 */
public class AdminNoteAction extends DispatchAction {
	
	NoteDao dao = new NoteImp();

	/** 
	 * Method execute 取所有日记
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 跳转到查看问题页	
	 */
	public ActionForward allNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		
		String sql = "notebook order by nb_id desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<NoteForm> list = dao.allNote(rs);
			// 数据放到request中
			request.getSession().setAttribute("allNote", list);
		}
		
			forword = mapping.findForward("successAll");
		return forword;
	}

	/** 
	 * Method execute 根据noteid查看日记
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 跳转到查看问题页	
	 */
	public ActionForward showNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		String nid = request.getParameter("noteid");
		int noteid = Integer.parseInt(nid); 
		//调用NoteDao类里面的showNote（）方法取到一个日记
		NoteForm nf = dao.showNote(noteid);
		if(nf != null){
			request.setAttribute("onenote", nf);
			forword = mapping.findForward("successshow");
		}else{
			forword = mapping.findForward("fail");
		}
		return forword;
	}

	/** 
	 * Method execute 根据noteid删除日记
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 跳转到查看问题页	
	 */
	public ActionForward delNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		String nid = request.getParameter("noteid");
		int noteid = Integer.parseInt(nid); 
		//调用NoteDao类里面的showNote（）方法删除一个日记
		boolean nf = dao.delNote(noteid);
		if(nf){
			forword = mapping.findForward("successDel");
		}
		request.setAttribute("message", "删除成功");
		return forword;
	}
}